package org.dtree.pv.sys.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.org.apache.bcel.internal.generic.DADD;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.dtree.pv.exception.UnauthorizedException;
import org.dtree.pv.framework.base.Result;
import org.dtree.pv.sys.models.MyTest;
import org.dtree.pv.sys.models.Sys_user;
import org.dtree.pv.sys.service.MyTestService;
import org.dtree.pv.sys.service.SysUserService;
import org.dtree.pv.util.JWTUtil;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
    @Reference
    MyTestService service;
    @Reference
    private SysUserService userService;

    @Autowired
    Dao dao;


    @RequestMapping("/test")
    @ResponseBody
    public Object test(){
        return service.test();
    }

    @RequestMapping("/test2")
    @ResponseBody
    public Object test2(){
        return dao.query(MyTest.class,null);
    }


    @GetMapping("/login")
    @ResponseBody
    public Object login(@RequestParam("username") String username,
                        @RequestParam("password") String password){
        try{
            Sys_user user = userService.getUserByUsername(username);
            password = new Sha256Hash(password, user.getSalt()).toHex();
            if (user.getPassword().equals(password)){
                System.out.println(password);
                return Result.success(200,"Login success", JWTUtil.sign(username,password));
            }else {
                throw new UnauthorizedException();
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new UnauthorizedException();
        }

    }


    @GetMapping("/article")
    @ResponseBody
    public Object article() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return new Result(200, "You are already logged in", null);
        } else {
            return new Result(200, "You are guest", null);
        }
    }

    @GetMapping("/require_auth")
    @RequiresAuthentication
    @ResponseBody
    public Object requireAuth() {
        return new Result(200, "You are authenticated", null);
    }

    @GetMapping("/require_role")
    @RequiresRoles("SUPERADMIN")
    @ResponseBody
    public Object requireRole() {
        return new Result(200, "You are visiting require_role", null);
    }

    @GetMapping("/require_permission")
    @RequiresPermissions(logical = Logical.OR, value = {"user","user.list"})
    @ResponseBody
    public Object requirePermission() {
        return new Result(200, "You are visiting permission require edit,view", null);
    }

}
